package pl.webapps.alarmme;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class OptionsActivity extends Activity {

    private TextView mVolumeText;
    private TextView mSnoozeText;
    private TextView mAutoSnoozeText;
    private SeekBar mVolumeBar;
    private SeekBar mSnoozeBar;
    private SeekBar mAutoSnoozeBar;
    private EditText mRepoEdit;
    private EditText mRingingTimeEdit;
    private CheckBox mUseMotionSensorCheckBox;
    private CheckBox mShowInNotificationCheckBox;
    private Button mSave;
    private Button mBack;
    private static final float VOLUME_RESOLUTION = 100;
    private static final int SNOOZE_RESOLUTION = 5;
    private static final int AUTO_SNOOZE_RESOLUTION = 10;
    private static final String LOG_TAG = "OptionsActivity";
    private OnClickListener mBtListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.options_save_button :
                    saveOptionsAction();
                    OptionsActivity.this.finish();
                    break;
                case R.id.options_cancel_button :
                    OptionsActivity.this.finish();
                    break;
            }
        }
    };
    private OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            switch (seekBar.getId()) {
                case R.id.options_volume_bar :
                    updateVolumeText(progress);
                    break;
                case R.id.options_snooze_bar :
                    updateSnoozeText(progress);
                    break;
                case R.id.options_auto_snooze_bar :
                    updateAutoSnoozeText(progress);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.options);
        prepareViewItems();
    }

    private void prepareViewItems() {
        mVolumeText = (TextView) findViewById(R.id.options_volume_label);
        mSnoozeText = (TextView) findViewById(R.id.options_snooze_label);
        mAutoSnoozeText = (TextView) findViewById(R.id.options_auto_snooze_label);
        mVolumeBar = (SeekBar) findViewById(R.id.options_volume_bar);
        mSnoozeBar = (SeekBar) findViewById(R.id.options_snooze_bar);
        mAutoSnoozeBar = (SeekBar) findViewById(R.id.options_auto_snooze_bar);
        mRepoEdit = (EditText) findViewById(R.id.options_repo_edit);
        mRingingTimeEdit = (EditText) findViewById(R.id.options_ringing_time_edit);
        mUseMotionSensorCheckBox = (CheckBox) findViewById(R.id.options_motion_sensor_check);
        mShowInNotificationCheckBox = (CheckBox) findViewById(R.id.options_show_in_notification_check);
        mSave = (Button) findViewById(R.id.options_save_button);
        mBack = (Button) findViewById(R.id.options_cancel_button);
        mVolumeBar.setProgress((int) (PreferencesHandler.loadAlarmVolume() * VOLUME_RESOLUTION));
        mSnoozeBar.setProgress(PreferencesHandler.loadSnoozeTimeDelta());
        mAutoSnoozeBar.setProgress(PreferencesHandler.loadAutoSnoozeTimeDelta());
        mRepoEdit.setText(PreferencesHandler.getSongsRepo());
        mRingingTimeEdit.setText(String.valueOf(PreferencesHandler.loadRingingTime()));
        mUseMotionSensorCheckBox.setChecked(PreferencesHandler.loadUseMotionSensor());
        mShowInNotificationCheckBox.setChecked(PreferencesHandler.loadShowInNotification());
        updateVolumeText(mVolumeBar.getProgress());
        updateSnoozeText(mSnoozeBar.getProgress());
        updateAutoSnoozeText(mAutoSnoozeBar.getProgress());
        mVolumeBar.setOnSeekBarChangeListener(mSeekListener);
        mSnoozeBar.setOnSeekBarChangeListener(mSeekListener);
        mAutoSnoozeBar.setOnSeekBarChangeListener(mSeekListener);
        mSave.setOnClickListener(mBtListener);
        mBack.setOnClickListener(mBtListener);
    }

    private void updateVolumeText(int progress) {
        mVolumeText.setText(getResources()
                .getString(R.string.options_volume_label)
                .concat(" ")
                .concat(String.valueOf(progress))
                .concat("%"));
    }

    private void updateSnoozeText(int progress) {
        mSnoozeText.setText(getResources()
                .getString(R.string.options_snooze_label)
                .concat(" ")
                .concat(String.valueOf((int) ((float) progress / (float) SNOOZE_RESOLUTION))));
    }

    private void updateAutoSnoozeText(int progress) {
        mAutoSnoozeText.setText(getResources()
                .getString(R.string.options_auto_snooze_label)
                .concat(" ")
                .concat(String.valueOf((int) ((float) progress / (float) AUTO_SNOOZE_RESOLUTION))));
    }

    private void saveOptionsAction() {
        setAlarmVolume((float) mVolumeBar.getProgress() / VOLUME_RESOLUTION);
        selectRepo(mRepoEdit.getText().toString());
        setSnoozeTime(mSnoozeBar.getProgress());
        setAutoSnoozeTime(mAutoSnoozeBar.getProgress());
        setRingingTime(Integer.parseInt(mRingingTimeEdit.getText().toString()));
        setUseMotionSensor(mUseMotionSensorCheckBox.isChecked());
        setShowInNotificationSensor(mShowInNotificationCheckBox.isChecked());
    }

    private void selectRepo(String repoName) {
        PreferencesHandler.setSongsRepo(repoName);
    }

    private void setSnoozeTime(int minutes) {
        PreferencesHandler.saveSnoozeTimeDelta(minutes);
    }

    private void setAutoSnoozeTime(int minutes) {
        PreferencesHandler.saveAutoSnoozeTimeDelta(minutes);
    }

    private void setRingingTime(int seconds) {
        PreferencesHandler.saveRingingTime(seconds);
    }

    private void setUseMotionSensor(boolean useMotionSensor) {
        PreferencesHandler.saveUseMotionSensor(useMotionSensor);
    }

    private void setShowInNotificationSensor(boolean showInNotification) {
        PreferencesHandler.saveShowInNotification(showInNotification);
    }

    private void setAlarmVolume(float volume) {
        // Log.d(LOG_TAG, "setAlarmVolume=" + volume);
        PreferencesHandler.saveAlarmVolume(volume);
    }
}
